﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;


public class UIManager
{
    #region 第一版：未抽取主prefab通用模板，界面缓存机制待完善,uiDepth未处理，注册模式未精简


    private static UIManager _instance;
    public static UIManager Instance
    {
        get
        {
            if (_instance == null)
                _instance = new UIManager();
            return _instance;
        }
    }
    /// <summary>
    /// 页面字典
    /// </summary>
    public static Dictionary<PageState, MyPageData.Data> UiDic { get; set; }
    /// <summary>
    /// 显示层级
    /// </summary>
    public static Dictionary<PageDepth, GameObject> UiPageDepth { get; set; }
    /// <summary>
    /// 注册界面
    /// </summary>
    public static void PageInit()
    {
        MyPageData _MyPageData = new MyPageData();
        _MyPageData.MyPageDataList = new List<MyPageData.Data>();
        //------------------在上面注册界面---------------------------------------
        ///——————————————————NNB代码
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.CreateRoomPlayerCountPage, FoldName = "SHPrefab", PerfabName = "CreateRoomPlayerCountPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.CreateRoomOneTimesBetPage, FoldName = "SHPrefab", PerfabName = "CreateRoomOneTimesBetPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.CreateRoomSameNiuPage, FoldName = "SHPrefab", PerfabName = "CreateRoomSameNiuPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.CreateRoomExplainPage, FoldName = "SHPrefab", PerfabName = "CreateRoomExplainPage" });
         _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.AppendScorePage, FoldName = "SHPrefab", PerfabName = "AppendScorePage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.CurrentWarRecordPage, FoldName = "SHPrefab", PerfabName = "CurrentWarRecordPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.RoomInfoPage, FoldName = "SHPrefab", PerfabName = "RoomInfoPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.RoomRulePage, FoldName = "SHPrefab", PerfabName = "RoomRulePage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.CreateRoomPage, FoldName = "SHPrefab", PerfabName = "CreateRoomPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.GameRoomInfoSet, FoldName = "SHPrefab", PerfabName = "GameRoomInfoSet" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() {pageState = PageState.PlayerInfoPage , FoldName = "SHPrefab", PerfabName = "PlayerInfoPage" });
        ///——————————————————比点加载预设体
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() {pageState = PageState.UserAgreeMentPage,FoldName = "SHPrefab", PerfabName = "UserAgreeMentPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.LoginPage, FoldName = "SHPrefab", PerfabName = "LoginPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.LoadingPage, FoldName = "SHPrefab", PerfabName = "LoadingPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.GameHallPage, FoldName = "Prefab", PerfabName = "GameHall" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.BuyRoomCardPage, FoldName = "SHPrefab", PerfabName = "BuyRoomCardPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.SharePage, FoldName = "SHPrefab", PerfabName = "SharePage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.NetConnectPage, FoldName = "SHPrefab", PerfabName = "NetConnectPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.GameSetPage, FoldName = "SHPrefab", PerfabName = "GameSetPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.ServicePage, FoldName = "SHPrefab", PerfabName = "ServicePage" });
      
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.JoinRoomPage, FoldName = "SHPrefab", PerfabName = "JoinRoomPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.GameRoomPage, FoldName = "SHPrefab", PerfabName = "GameRoomPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.BetPage, FoldName = "SHPrefab", PerfabName = "BetPage"});
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.ChapterEndPage, FoldName = "SHPrefab", PerfabName  = "ChapterEndPage" });
;       _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.GameEndPage, FoldName = "SHPrefab", PerfabName = "GameEndPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.VoteDissolveRoomPage, FoldName = "SHPrefab", PerfabName = "VoteDissolveRoomPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.WarRecordPage, FoldName = "SHPrefab", PerfabName = "WarRecordPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.WarDetailsPage, FoldName = "SHPrefab", PerfabName = "WarDetailsPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.TrusteeShipPage, FoldName ="SHPrefab",PerfabName = "TrusteeShipPage" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.FaXian, FoldName = "Prefab/FaXian", PerfabName = "FaXian" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.ZhanJi, FoldName = "Prefab/ZhanJi", PerfabName = "ZhanJipanel" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.ZhanjiList, FoldName = "Prefab/ZhanJi", PerfabName = "Zhanji" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.Club, FoldName = "Prefab/Clubs", PerfabName = "MyClub" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.Shop, FoldName = "Prefab/Shop", PerfabName = "ShopIng" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.Gy, FoldName = "Prefab/Me", PerfabName = "GY" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.Set, FoldName = "Prefab/Me", PerfabName = "Set" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.LiuYan, FoldName = "Prefab/Me", PerfabName = "LiuYan" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.GetMe, FoldName = "Prefab/Me", PerfabName = "GetMe" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.IfMyClub, FoldName = "Prefab/Clubs", PerfabName = "IfMyClub" }); 
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.IfMyLM, FoldName = "Prefab/Legends", PerfabName = "IfMyLM" }); 
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.Checks, FoldName = "Prefab/Clubs", PerfabName = "Checks" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.ChecksLm, FoldName = "Prefab/Legends", PerfabName = "ChecksLm" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.OtherClubList, FoldName = "Prefab/Clubs", PerfabName = "OtherClubList" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.OtherLMList, FoldName = "Prefab/Legends", PerfabName = "OtherLmList" });

        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.ChangeMoney, FoldName = "Prefab/Shop", PerfabName = "ShopMoney" });
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.LM, FoldName = "Prefab/Legends", PerfabName = "MyLegends" });
        //
        _MyPageData.MyPageDataList.Add(new MyPageData.Data() { pageState = PageState.ChangeAccount, FoldName = "Prefab/TiSi", PerfabName = "ChangeAccountPopupPage" });
        UiDic = _MyPageData.MyPageDataList.ToDictionary((p) => p.pageState, (q) => q);
        Debug.Log("加载ui配置表完成"); 
    }

    /// <summary>
    /// 页面切换
    /// </summary>
    /// <param name="state">切换目标</param>
    /// <param name="callBack">切换回调</param>
    public delegate void CallBack(GameObject child);
    public static void ChangeUI(PageState state, CallBack callBack = null, float delayTime = -1)
    {
        ChangeUiByIEnumerator(state,callBack,delayTime);
    }

    public static void  ChangeUiByIEnumerator(PageState state, CallBack callBack = null, float delayTime = -1)
    {
        ///切换目标对象
        MyPageData.Data data = UiDic[state];
        GameObject father = null;

        //如果是断线重连或者退出面板
        if (data.PerfabName == "NetConnectPage" || data.PerfabName == "ExitGamePanel")
        {
            father = GameObject.Find("specialAddNode");
        }
        else if (data.PerfabName == "VoteDelRoomPanel" || data.PerfabName == "GameEndPage" || data 
            .PerfabName == "BetPage" || data.PerfabName == "ChapterEndPage" || data.PerfabName == "CurrentWarRecordPage" || data.PerfabName == "AppendScorePage"
            || data.PerfabName == "GameRoomInfoSet" || data.PerfabName == "RoomRulePage" || data.PerfabName == "RoomInfoPage" || data.PerfabName == "PlayerInfoPage")
        {
            father = GameObject.Find("GameRoomPage(Clone)/FightAddNode");
        }
        else
        {
            father = GameObject.Find("addNode");
        }

        //Debug.Log("changeUI加载预设体");
        //Debug.Log(data.FoldName);
        //Debug.Log(data.PerfabName);

        //如果这个面板已经生成过了不再生成
        if (data.PerfabName!= "LoginPage")
        {
            if (GameObject.Find(data.PerfabName + "(Clone)") && data.PerfabName != "AppendScorePage")
            {
                Debug.Log("这个面板已经生成过了" + data.PerfabName);
                return;
            }
        }
        GameObject obj = GameObject.Instantiate(Resources.Load<GameObject>(string.Format("{0}/{1}", data.FoldName, data.PerfabName)));
        try
        {
            EventManager eventManager = obj.GetComponent<EventManager>();
            obj.GetComponent<EventManager>().templateEventManager = eventManager;
            obj.GetComponent<EventManager>().templateEventManager.Open();
        }
        catch (System.Exception ex)
        {
            Debug.LogError(obj.name);
            throw ex;
        }
        ///坐标位置 拉伸适应
        SetAnchorPoint(obj, father);
        UpdateUiInfor(callBack, obj);
        callBack = null;
    }

    //显示对话框
    public void ShowDialog(string msg, DialogType type = DialogType.None)
    {
    }

    public static void UpdateUiInfor(CallBack callBack, GameObject obj)
    {
        //yield return new WaitForEndOfFrame();
        try
        {
            if (callBack != null)
            {
                callBack(obj);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            callBack = null;
        }
    }
    /// <summary>
    /// 初始化新生成目标的锚点与缩放
    /// </summary>
    /// <param name="obj">目标</param>
    /// <param name="father">参照对象</param>
    public static void SetAnchorPoint(GameObject prefab, GameObject parentNode)
    {
        prefab.transform.SetParent(parentNode.transform,false);
        prefab.transform.localPosition = new Vector3(0, 0, 0);
        prefab.transform.localRotation = parentNode.transform.rotation;
        prefab.transform.localScale = parentNode.transform.localScale;
    }
    /// <summary>
    /// 关闭页面统一接口
    /// </summary>
    /// <param name="page"></param>
    public static void ClosePage(PageState page)
    {
        //if (UiCache.Contains(page))
        //{
        //    UiDic[page].ClosePage();
        //    UiCache.Remove(page);
        //}
    }
    #endregion
    /// <summary>
    /// 页面
    /// </summary>
    public enum PageState
    {
        //--------------------------牛牛吧
        CreateRoomPlayerCountPage,
        CreateRoomSameNiuPage,
        CreateRoomOneTimesBetPage,
        CreateRoomExplainPage,
        AppendScorePage,
        CurrentWarRecordPage,
        RoomInfoPage,
        RoomRulePage,
        GameRoomInfoSet,
        PlayerInfoPage,
        ///——————————比点面板
        GameRoomPage,
        NetConnectPage,
        LoginPage,
        UserAgreeMentPage,
        LoadingPage,
        GameHallPage,
        BuyRoomCardPage,
        SharePage,
        ServicePage,
        GameSetPage,
        CreateRoomPage,
        JoinRoomPage,
        WarRecordPage,
        WarDetailsPage,
        ChapterEndPage,
        GameEndPage,
        BetPage,                       //下注面板
        VoteDissolveRoomPage,
        TrusteeShipPage,
        FaXian,
        Club,
        OtherClubList,
        OtherLMList,
        ZhanJi,
        ZhanjiList,
        MyPanel,
        Shop,
        Gy,//关于牛牛吧
        Set,
        LiuYan,
        GetMe,
        IfMyClub,
        IfMyLM,
        ChangeMoney,
        Checks,//会员申请列表面板
        ChecksLm,
        LM,
        ChangeAccount,
        //----------在上面添加-----------------------------
        /// 最大值 不允许超过该值 枚举最大值
        /// </summary>
        ///</summary>
        Max,
    }
    /// <summary>
    /// 页面显示层级关系
    /// </summary>
    public enum PageDepth
    {
        None = 0,
        SystemPage = 8,
        Max
    }
    /// <summary>
    /// 对话框类型
    /// </summary>
    public enum DialogType
    {
        None = 0,
    }
    public static List<GameObject> GetChild(GameObject obj)
    {
        List<GameObject> list = new List<GameObject>();
        int i = 0;
        while (i < obj.transform.childCount)
        {
            Transform parent = obj.transform.GetChild(i);
            list.Add(parent.gameObject);
            i++;
        }
        return list;
    }
}
public class MyPageData
{
    public List<Data> MyPageDataList
    {
        get;
        set;
    }

    public class Data
    {
        public UIManager.PageState pageState { get; set; }
        private UIManager.PageDepth PageDepth { get; set; }
        public string FoldName { get; set; }
        public string PerfabName { get; set; }
    }

}
